package com.mvetc.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.mvetc.entity.Movies;
import com.mvetc.entity.PageInfo;
import com.mvetc.service.AdminService;
import com.mvetc.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       AdminService adminService=new AdminServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Gson gson=new Gson();
		HttpSession session=request.getSession();
		Map<String, Object> map=new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		String search=request.getParameter("Searchfor");
		if (search!=null) {
			session.setAttribute("search", search);
		}
		
		
		
		String op=request.getParameter("op");
		if ("addCount".equals(op)) {
			String id=request.getParameter("id");
 int n=adminService.addCountById(id);
			map.put("movies", null);
		}
		if ("findbyname".equals(op)) {
			String pageNum=request.getParameter("pageNum");
			String pageSize=request.getParameter("pageSize");
			  String name=(String) session.getAttribute("search");
			 
			  PageInfo<Movies> movies=adminService.findBy(name, Integer.parseInt(pageNum),Integer.parseInt(pageSize));
			map.put("movies", movies);
		}
		if ("findAll".equals(op)) {
			String pageNum=request.getParameter("pageNum");
			String pageSize=request.getParameter("pageSize");
			  
			 
			  PageInfo<Movies> movies=adminService.findAll(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
			map.put("movies", movies);
			
		}
		if ("findBySpell".equals(op)) {
			String pageNum=request.getParameter("pageNum");
			String pageSize=request.getParameter("pageSize");
			String spell=request.getParameter("spell");
			 session.setAttribute("movieSpell", spell);
			 
			  PageInfo<Movies> movies=adminService.findBySpell(spell,Integer.parseInt(pageNum),Integer.parseInt(pageSize));
			map.put("movies", movies);
			
		}
		if ("findBySpell2".equals(op)) {
			String pageNum=request.getParameter("pageNum");
			String pageSize=request.getParameter("pageSize");
			
			 String movieSpell=(String) session.getAttribute("movieSpell");
			 
			  PageInfo<Movies> movies=adminService.findBySpell(movieSpell,Integer.parseInt(pageNum),Integer.parseInt(pageSize));
			map.put("movies", movies);
			
		}if("searchMovieByType".equals(op)){
			map.put("movies", null);
		}
		if("searchMovieByArea".equals(op)){
			map.put("movies", null);
		}
		 response.setContentType("application/json;charset=utf-8");
		 String jsonObject=gson.toJson(map);
		 response.getWriter().write(jsonObject);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
